package com.dmcb.trade.business.utils;

import com.dmcb.common.business.utils.EncodeUtil;
import com.dmcb.common.business.utils.WebClientUtil;
import com.gargoylesoftware.htmlunit.WebClient;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.HashMap;
import java.util.Map;

/**
 * created by 王勇 on 2018/10/19.
 */
public class TouTiaoCrawlUtil {


    public static Map<String, String> getTouTiaoInfo(String link) {
        WebClientUtil webClientUtil = new WebClientUtil();
        WebClient webClient = webClientUtil.initWc(false, false, true);
        String data = webClientUtil.getDoc2(webClient,link).toString();
        Map<String, String> map = new HashMap<>();
        if (data.contains("articleInfo:")) {
            map = getMapData(data);
        } else if (data.contains("galleryInfo =")) {
            map = getMapDataByImg(data);
        }else if(data.contains("404")){
            return null;
        }
        return map;
    }

    //不加载JS,
    private static  Map<String, String> getMapData(String data) {
        Document doc;
        data = data.substring(data.indexOf("articleInfo:") + 12);
        data = "{" + data.substring(data.indexOf("title"));
        String title = getData("title: '", "',", data);
        String content = getData("content: '", "',", data);
        content = content.replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&").replace("&quot;", "\"");
        if (StringUtils.isBlank(content)) {
            return null;
        }
        doc = Jsoup.parse("<html>" + content + "</html>");
        content = doc.text();
        //获取时间信息
        String authorName = getData("source: '", "',", data);
        //获取作者信息
        String sTime = getData("time: '", "}", data);
        if (StringUtils.isBlank(title)) {
            return null;
        }
        Map<String, String> map = new HashMap<>();
//        map.put("authorName", authorName);
//        map.put("time", sTime);
        map.put("title", title);
//        map.put("content", content);
        return map;
    }

    /**
     * 解析头条文章  多页图片类型
     *
     * @param data 获取的数据
     * @return 结果
     */
    private static  Map<String, String> getMapDataByImg(String data) {
        //为了获取作者信息截取第一次
        data = data.substring(data.indexOf("mediaInfo"));
        //获取作者信息
        String authorName = getData("name: '", "',", data);
        //为了获取标题信息，第二次截取
        data = data.substring(data.indexOf("galleryInfo ="));
        //获取标题信息
        String title = getData("title: '", "',", data);
        if (StringUtils.isBlank(title)) {
            return null;
        }

        //获取时间信息
        String sTime = getData("publish_time: '", "',", data);
        //为了获取剩余信息，第三次截取
        data = data.replace("\\\\\\\"", "\"").replace("\\\"", "\"");
        data = data.substring(data.indexOf("sub_abstracts\":") + 16);
        String content = data.substring(0, data.indexOf("]}\"),")).replace("\\\\", "\\");
        if (StringUtils.isBlank(content)) {
            return null;
        }
        content = EncodeUtil.decode(content);
        Map<String, String> map = new HashMap<>();
        map.put("authorName", authorName);
        map.put("time", sTime);
        map.put("title", title);
        map.put("content", content);
        return map;

    }

    //针对加载不了JS的头条文章获取数据的方法
    public static String getData(String start, String end, String data) {
        data = data.substring(data.indexOf(start) + start.length());
        return data.substring(0, data.indexOf(end)).trim();
    }


   /* public static void main(String[] args) {

        System.out.println(getTouTiaoInfo("https://www.toutiao.com/a6617589894136988163/"));
    }*/
}
